<template>
  <ea-dialog
    v-bind="$attrs"
    v-on="$listeners"
    width="400px"
    ref="elDialog"
    :title="title"
    :type="type"
    @submit="submit"
    @reset="resetClear"
    @closed="handleClosed"
  >
    <el-form ref="form" :rules="rules" :model="singleData" label-width="100px">
      <el-form-item label="所属科目" prop="subid">
        <el-select
          placeholder="请选择科目"
          clearable
          v-model="singleData.subid"
        >
          <el-option
            v-for="item in subjects"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="章节名称" prop="chaptername">
        <el-input
          v-model="singleData.chaptername"
          placeholder="请输入章节名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="所属区域" prop="area">
        <SelectCity
          @proClick="proClick"
          @cityClick="cityClick"
          :Province="singleData.provinceId"
          :cityCode="singleData.cityId"
        ></SelectCity>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input
          v-model="singleData.description"
          placeholder="请输入描述"
        ></el-input>
      </el-form-item>
      <el-form-item label="排序号" prop="sort">
        <el-input v-model="singleData.sort" placeholder="请输入排序"></el-input>
      </el-form-item>
    </el-form>
  </ea-dialog>
</template>

<script>
import editAndAdd from "@/mixins/business/editAndAdd";
import SelectCity from "@/views/component/selectCity";
export default {
  name: "EaChapter",
  props: {
    subjects: Array,
    dialogvisible: Boolean
  },
  mixins: [editAndAdd],
  components: { SelectCity },
  data() {
    return {
      moduleName: "章节",
      singleData: {
        chapterid: 0,
        chaptername: "",
        description: "",
        subid: "",
        provinceId: "",
        cityId: ""
      },
      ProvChose: "",
      cityChose: "",
      rules: {
        subid: { required: true, message: "请选择所属科目！" },
        chaptername: { required: true, message: "请输入章节名称！" }
      }
    };
  },
  watch: {
    singleData() {
      this.cityChose = [];
      if (this.singleData.provinceId) {
        this.cityChose.push(this.singleData.provinceId, this.singleData.cityId);
      }
    },
    dialogvisible: {
      handler(n) {
        this.cityChose = [];
        this.singleData.provinceId = "";
        this.singleData.cityId = "";
      }
    }
  },
  methods: {
    resetClear() {
      this.cityChose = [];
      this.singleData.provinceId = "";
      this.singleData.cityId = "";
      this.$refs.form.resetFields();
    },
    handle() {
      if (this.type === "add") this.singleData.chapterid = null;
      return this.axios.post(
        "/sys/chapter/addOrUpdateChapter",
        this.singleData
      );
    },
    proClick(e) {
      //从城市组件接收到的数据
      this.singleData.provinceId = e;
    },
    cityClick(e) {
      //从城市组件接收到的数据
      this.singleData.cityId = e;
    },
    handleClosed() {
      this.cityChose = [];
      this.end();
    }
  }
};
</script>

<style scoped></style>
